export { Message, Modal } from '@arco-design/web-vue'
import '@arco-design/web-vue/es/message/style/index.less'
import '@arco-design/web-vue/es/modal/style/index.less'
export { get, post, request } from './request'
import { getToken } from './auth'
import { Message, MessageMethod } from '@arco-design/web-vue'
import { BtnPermission } from '@/enum/permission'
import { useAppStore } from '@/store'
type TargetContext = '_self' | '_parent' | '_blank' | '_top'

type MessageType = keyof MessageMethod
type MessageOptions = {
  type?: MessageType
  content: string
  position?: 'top' | 'bottom'
  duration?: number
  cb?: Function
}
export const message = (msgOptions: MessageOptions) => {
  const query: MessageOptions = {
    duration: 1500,
    type: 'success',
    ...msgOptions,
  }
  Message[query.type as any]({
    ...query,
    onClose: () => query.cb?.(),
  })
}

export const openWindow = (url: string, opts?: { target?: TargetContext; [key: string]: any }) => {
  const { target = '_blank', ...others } = opts || {}
  window.open(
    url,
    target,
    Object.entries(others)
      .reduce((preValue: string[], curValue) => {
        const [key, value] = curValue
        return [...preValue, `${key}=${value}`]
      }, [])
      .join(',')
  )
}
/**随机颜色 */
export const randomHexColor = () =>
  `#${Math.floor(Math.random() * 0xffffff)
    .toString(16)
    .padEnd(6, '0')}`
/**16进制颜色转rbga */
export const hexTorgba = hex => {
  if (!hex) return
  let str = hex.replace('#', '')
  if (str.length % 3) {
    return 'hex格式不正确！'
  }
  //获取截取的字符长度
  let count = str.length / 3
  //根据字符串的长度判断是否需要 进行幂次方
  let power = 6 / str.length
  let r = parseInt('0x' + str.substring(0 * count, 1 * count)) ** power
  let g = parseInt('0x' + str.substring(1 * count, 2 * count)) ** power
  let b = parseInt('0x' + str.substring(2 * count)) ** power
  return `${r},${g},${b}`
}
export const regexUrl = new RegExp(
  '^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
  'i'
)
/**
 * 下载文件
 * @param url 请求地址
 * @param obj  拼接对象
 */
export const download = (url, obj = {}) => {
  let str = ''
  Object.keys(obj).forEach(key => {
    str += `${key}=${obj[key]}&`
  })
  const api = import.meta.env.MODE === 'development' ? 'http://xpj40szpt.signalpha.cn/' : '/'
  openWindow(`${api}api${url}?${str}token=bearer ${getToken()}&Authorization=c2VsbGVyOjQwV0hueEkxUA==`)
}

/**枚举转table用数据 */
export const formatEnum = enumObj => {
  var pattern = new RegExp('[0-9]+')
  const valueStatus = {}
  Object.keys(enumObj).forEach(key => {
    if (pattern.test(enumObj[key])) {
      valueStatus[enumObj[key]] = { text: key }
    }
  })
  return valueStatus
}

/**枚举转select用数据
 * @param enumObj 枚举
 * @param isNum 是否数字类型
 */
export const formatOptions = (enumObj, isNum = true) => {
  var pattern = new RegExp('[0-9]+')
  const options: { label: string; value: string }[] = []
  Object.keys(enumObj).forEach(key => {
    if (!isNum || pattern.test(enumObj[key])) {
      options.push({
        label: key,
        value: enumObj[key],
      })
    }
  })
  return options
}

/**判断按钮级权限 */
export const checkBtnPermission = (preStr: BtnPermission) => {
  const res = (useAppStore().getCurrentPageButton as any).map(item => item.code.split('_')[1])
  if (res.every(v => v != preStr)) {
    message({
      type: 'warning',
      content: '暂无权限进行此操作',
    })
  } else return true
}
export const loadFile = file => 'https://' + useAppStore().imgUrl + file
export default null

/**转换列表sort字段传给java */
export const formatSortParams = (sort = {}) => {
  const query: any = {}

  Object.entries(sort).forEach(([field, direction]) => {
    if (direction === 'ascend') {
      query.ascs = query.ascs ? `${query.ascs},${field}` : field
    } else if (direction === 'descend') {
      query.descs = query.descs ? `${query.descs},${field}` : field
    }
  })

  return query
}
